<template>
  <!-- 已经支付订单 -->
  <div class="app-container">
    <!-- 筛选 -->
    <div class="filter-container">
      <el-form :inline="true" :model="listQuery">
        <!-- 支付渠道 -->
        <el-form-item>
          <el-select
            v-model="listQuery.payChannel"
            placeholder="支付渠道"
            style="width: 120px"
            clearable
          >
            <el-option
              v-for="item in payChannelOptions"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 时间范围 -->
        <el-form-item>
          <el-date-picker
            v-model="listQuery.datetimerange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="支付开始日期"
            end-placeholder="支付结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="pickerOptions"
          />
        </el-form-item>
        <!-- 关键字 -->
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          ></el-button>
        </el-form-item>
      </el-form>
      <div style="line-height: 36px">
        <span class="mr10">
          订单总金额：<strong>{{ sumOrderAmount }}</strong>
        </span>
        <el-button
          :loading="downloadLoading"
          type="primary"
          icon="el-icon-download"
          @click="handleDownload"
        >
          导出
        </el-button>
      </div>
    </div>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      size="mini"
    >
      <el-table-column label="#" type="index" width="50" />
      <el-table-column label="订单编号">
        <template slot-scope="{ row }">
          <div>
            <el-link
              type="primary"
              :underline="false"
              @click="$router.push(`/order/detail/${row.id}`)"
              >{{ row.orderNum }}</el-link
            >
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品信息" width="220">
        <template slot-scope="{ row }">
          <div class="goodinfo">
            <!-- <img src="/demo.png" alt="" style="width: 220px" /> -->
            <div><img :src="getFile(row.worksCover)" alt="" style="width: 80px" /></div>
            <div class="desc">
              <p>{{ row.worksName }}</p>
              <!-- 定价销售才有艺术家 -->
              <p v-if="row.buyType == 0">艺术家：{{ row.artUserName }}</p>
              <p>{{ rightOptionsKV[row.tubeType] }}</p>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="销售类型" prop="type" width="100">
        <template slot-scope="{ row }">
          <div>{{ buyTypeKV[row.buyType] }}</div>
        </template>
      </el-table-column>
      <el-table-column label="单价" prop="unitPrice" />
      <el-table-column label="数量" prop="buyNum" />
      <el-table-column label="总额" prop="orderAmount" />
      <!-- <el-table-column label="单价 * 数量 = 交易金额" prop="unitPrice">
        <template slot-scope="{ row }">
          <div>{{ row.unitPrice }} * {{ row.buyNum }} = {{ row.orderAmount }}</div>
        </template>
      </el-table-column> -->
      <el-table-column label="支付方式" prop="payChannel" />
      <el-table-column label="客户" prop="">
        <template slot-scope="{ row }"> {{ row.mobile }}({{ row.name }})</template>
      </el-table-column>
      <el-table-column label="状态" prop="" width="120">
        <template slot-scope="{ row }">
          <el-tag :type="tradeStatusTag[row.tradeStatus]">{{
            tradeStatusKV[row.tradeStatus]
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单时间" prop="createTime" />
      <el-table-column label="支付时间" prop="payTime" />
    </el-table>
    <!-- 分页 -->
    <div class="m-center">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="fetchList"
      />
    </div>
  </div>
</template>

<script>
import { fetchPayList, querySumPayAmount, exportPay } from "@/api/order";
import { downloadBlobFile } from "@/utils";
import {
  payChannelOptions,
  payChannelOptionsKV,
  buyTypeOptions,
  buyTypeKV,
  tradeStatusOptions,
  tradeStatusKV,
  tradeStatusTag,
  rightOptionsKV,
} from "@/utils/options";
import { getFile } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination

export default {
  name: "SettlementBill",
  components: { Pagination },
  data() {
    return {
      listLoading: false, // 加载动画
      tableData: [],
      total: 0,
      listQuery: {
        endTime: "", // 支付查询结束时间
        limit: 5,
        page: 1,
        startTime: "", // 支付查询开始时间
        payStatus: 2, // 支付状态，0:未付款，1:付款中，2:付款成功，3:付款失败
        datetimerange: [],
        marketType: 0, //	市场类型(0:上架市场，1:寄售市场)
        payChannel: null, // 支付渠道(0:联动支付,1:衫德支付)
      },
      payChannelOptions,
      payChannelOptionsKV,
      tradeStatusOptions,
      tradeStatusKV,
      tradeStatusTag,
      buyTypeOptions,
      buyTypeKV,
      rightOptionsKV,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      sumOrderAmount: 0,
      downloadLoading: false,
      timer: null,
    };
  },

  created() {
    this.fetchList();
  },
  methods: {
    /*  增删改查--start */
    // 得到列表
    async fetchList() {
      this.listLoading = true;
      this.getTime();
      fetchPayList(this.listQuery).then((response) => {
        this.tableData = response.data.items;
        this.total = response.total;
        this.listLoading = false;

        this.querySumOrderAmount();
      });
    },

    getTime() {
      const { datetimerange } = this.listQuery;
      if (datetimerange && datetimerange.length === 2) {
        this.listQuery.startTime = datetimerange[0];
        this.listQuery.endTime = datetimerange[1];
      } else {
        this.listQuery.startTime = "";
        this.listQuery.endTime = "";
      }
    },

    // 搜索
    handleFilter() {
      this.listQuery.page = 1;
      this.fetchList();
    },
    querySumOrderAmount() {
      querySumPayAmount(this.listQuery).then((response) => {
        this.sumOrderAmount = response;
      });
    },

    /*  增删改查--end */
    getFile,

    handleDownload() {
      this.downloadLoading = true;
      this.getTime();
      const { endTime, payStatus, startTime, marketType, payChannel } = this.listQuery;
      exportPay({ endTime, payStatus, startTime, marketType, payChannel }).then((res) => {
        this.exportQuery({ code: res });
        this.timer = setInterval(() => {
          this.exportQuery({ code: res });
        }, 2000);
      });
    },
    // exportQuery(data) {
    //   //     code	编码	string
    //   // stage	阶段(0: 准备导出 1: 正在导出 2: 导出成功 3: 导出失败	string
    //   // url	下载地址
    //   exportQuery(data).then((res) => {
    //     if (res.stage === "2") {
    //       this.downloadLoading = false;
    //       this.$message.success("导出成功");
    //       clearInterval(this.timer);
    //       downloadFile({ ossKey: res.url }).then((response) => {
    //         const blob = response;
    //         const fileName = "已支付订单" + res.url;

    //         downloadBlobFile(blob, fileName);
    //       });
    //     } else if (res.stage === "3") {
    //       this.$message.error("导出失败");
    //       clearInterval(this.timer);
    //     }
    //   });
    // },
  },
};
</script>
<style lang="scss" scoped>
.goodinfo {
  display: flex;
  img {
    width: 80px;
    margin-right: 8px;
  }
  .desc {
    text-align: left;
  }
  p {
    margin: 5px 0;
  }
}
</style>
